package SingleLinkedList;

public class MySingleLinkedList implements ISingleLinkedList{

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode() {
        }
    }

    public ListNode head;

    public void createList() {
        ListNode node1=new ListNode(12);
        ListNode node2=new ListNode(23);
        ListNode node3=new ListNode(34);
        ListNode node4=new ListNode(45);
        ListNode node5=new ListNode(56);

        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;

        this.head=node1;
    }

    @Override
    public void display(ListNode newH) {
        ListNode cur=newH;
        while(cur!=null) {
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    @Override
    public int size() {
        ListNode cur=head;
        int count=0;
        while(cur!=null) {
            cur=cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void addFirst(int val) {
        ListNode node=new ListNode(val);
//        if(head==null) {
//            head=node;
//            return;
//        }
        node.next=head;
        head=node;
    }

    @Override
    public void addLast(int val) {
        ListNode node=new ListNode(val);
        if(head==null) {
            head=node;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null) {
            cur=cur.next;
        }
        cur.next=node;
    }

    @Override
    public void addIndex(int index, int val) {
        try{
            checkLegalOfIndex(index);
        }catch(IndexNotLegalException e) {
            e.printStackTrace();
        }
        if(index==0) {
            addFirst(val);
            return;
        }
        else if(index==size()) {
            addLast(val);
            return;
        }
        ListNode node=new ListNode(val);
        ListNode cur=findIndexSubOne(index);
        node.next=cur.next;
        cur.next=node;
    }

    public void checkLegalOfIndex(int index) {
        if(index<0 || index>size()) {
            throw new IndexNotLegalException("Index位置不合法！");
        }
    }

    public ListNode findIndexSubOne(int index) {
        ListNode cur=head;
        for(int i=1;i<=index-1;i++) {
            cur=cur.next;
        }
        return cur;
    }

    @Override
    public boolean contains(int val) {
        ListNode cur=head;
        while(cur.next!=null) {
            if(cur.val==val) {
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int val) {
        //如果head为空
        if(head==null) {
            return;
        }
        if(!contains(val)) {
            System.out.println("不存在该值");
            return;
        }
        if(head.val==val) {
            head=head.next;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null) {
            if(cur.next.val==val) {
                cur.next=cur.next.next;
                return;
            }
            cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int val) {
        //如果head为空
        if(head==null) {
            return;
        }
        ListNode prev=head;
        ListNode cur=prev.next;
        while(cur!=null) {
            if(cur.val==val) {
                prev.next=cur.next;
            }
            else {
                prev=cur;
            }
            cur=cur.next;
        }
        if(head.val==val) {
            head=head.next;
        }
    }

    @Override
    public void clear() {
        head=null;
    }


    public ListNode reverseList() {
        if(head==null)return head;
        ListNode cur=head.next;
        head.next=null;

        while(cur!=null) {
            ListNode curN=cur.next;
            //开始翻转
            cur.next=head;
            head=cur;
            cur=curN;
        }
        return head;
    }

    public ListNode middleNode() {
        ListNode cur=head;
        int count=0;
        while(cur!=null) {
            cur=cur.next;
            count++;
        }
        System.out.println(count);
        int ret=count/2;
        ListNode cur2=head;
        for(int i=1;i<=ret;i++) {
            cur2=cur2.next;
        }
        return cur2;
    }

    public ListNode middleNode1() {
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null && fast.next!=null) {
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;
    }

    //返回倒数第k个节点，判断k的合法性
    //不能求长度，只允许遍历一遍链表
    public int kthToLast(int k) {
        if(k<=0 || head==null) {
            return -1;
        }
        ListNode fast=head;
        ListNode slow=head;
        for(int i=1;i<k;i++) {
            if(fast.next==null) {
                return -1;
            }
            fast=fast.next;
        }
        while(fast.next!=null) {
            fast=fast.next;
            slow=slow.next;
        }
        return slow.val;
    }

    /*
1.最后一个节点需要指定指向为null
2.所有节点一定都小于x吗？全部大于x呢？全部小于x呢？
若全部小于，那么第一个链表为空，那不就空指针异常了？
*/
    public ListNode partition(int x) {
        // write code here
        if(head==null){
            return null;
        }
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = head;
        while (cur != null) {
            if (cur.val < x) {
                //第一次插入
                if (bs == null) {
                    bs = be = cur;
                } else {
                    //不是第一次插入
                    be.next = cur;
                    be = be.next;
                }
                cur = cur.next;
            } else {
                //第一次插入
                if (as == null) {
                    as = ae = cur;
                } else {
                    //不是第一次插入
                    ae.next = cur;
                    ae = ae.next;
                }
                cur = cur.next;
            }
        }
        //第一部分是不是空的，如果空直接返回第二部分，不为空进行拼接
        if(bs==null) {
            return as;
        }
        be.next=as;
        //后半部分不为空，最后一个节点置空
        if(as!=null) {
            ae.next=null;
        }
        return bs;
    }

}
